﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppDesigner.XamlDom
{
    public class CollectionElementsCollection : Collection<XamlPropertyValue>
    {
        #region Fields

        private XamlProperty _property;

        #endregion

        #region Constructor

        public CollectionElementsCollection(XamlProperty property)
        {
            _property = property;
        }

        #endregion

        #region Override

        public void AddInternal(XamlPropertyValue value)
        {
            base.InsertItem(this.Count, value);
        }

        protected override void InsertItem(int index, XamlPropertyValue item)
        {
            XamlPropertyInfo info = _property.PropertyInfo;
            object collection = info.GetValue(_property.ParentObject.Instance);
            CollectionSupport.Insert(info.ReturnType, collection, item, index);
            item.ParentProperty = _property;
            _property.InsertNodeInCollection(item.GetNodeForCollection(), index);
            base.InsertItem(index, item);
        }

        #endregion
        
    }
}
